//
//  WACustomCollectionView.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 04.09.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "WACustomCollectionView.h"
#import "NSScrollView+AnimatedScroll.h"

@interface WACustomCollectionView ()

@property (nonatomic) CGFloat scrollingXDelta;

@end

@implementation WACustomCollectionView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(NSUInteger)itemIndexAtLocation:(CGPoint)location
{
    NSUInteger idx = NSNotFound;
    
    // check what item contains this location
    NSView *itemView = [self hitTest:location];
    if (itemView) {
        
        for (int i=0; i<self.content.count; i++) {
            
            if (CGRectContainsPoint([self frameForItemAtIndex:i], location)) {
                idx = i;
                break;
            }
        }
    }
    
    return idx;
}

-(void)setContent:(NSArray *)content
{
    [super setContent:content];
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    
    // Drawing code here.
}

-(void)scrollWheel:(NSEvent *)theEvent
{
    if (theEvent.phase == NSEventPhaseChanged) {
        // get direction of scroll and return
        self.scrollingXDelta = theEvent.scrollingDeltaX;
        return;
    }
    
    if (theEvent.phase == NSEventPhaseEnded) {

        // if gesture is done
        // and direction is present
        if (self.scrollingXDelta == 0)
            return;
        
        [self scrollToDirection:copysignf(1.0, self.scrollingXDelta)];
        
//        NSRect visRect = self.visibleRect;
//        
//        NSRect itemFrame = [self frameForItemAtIndex:[self itemIndexAtLocation:NSMakePoint(visRect.origin.x+100, 0)]];
//        CGFloat x = itemFrame.origin.x + copysignf(1.0, self.scrollingXDelta) * (-itemFrame.size.width) + (itemFrame.size.width-visRect.size.width);
        self.scrollingXDelta = 0;
//        // if trying to scroll out of collection - return
//        if (x+itemFrame.size.width > self.frame.size.width) {
//            
//            return;
//        }
//        // prevent scroll to negative position
//        x = (x>0) ? x : 0;
//        
//        [self.enclosingScrollView scrollToXPosition:x];
        
    }
}


-(void)scrollToNextItem
{
    [self scrollToDirection:-1];
}
-(void)scrollToPreviousItem
{
    [self scrollToDirection:1];
}

-(void)scrollToDirection:(NSInteger)direction
{
    NSRect visRect = self.visibleRect;
    
    NSRect itemFrame = [self frameForItemAtIndex:[self itemIndexAtLocation:NSMakePoint(visRect.origin.x+100, 0)]];
    CGFloat x = itemFrame.origin.x + direction * (-itemFrame.size.width) + (direction<0 ? 0 : (itemFrame.size.width-visRect.size.width) );

    // if trying to scroll out of collection - return
    if (x+itemFrame.size.width > self.frame.size.width) {
        
        return;
    }
    // prevent scroll to negative position
    x = (x>0) ? x : 0;
    
    [self.enclosingScrollView scrollToXPosition:x];
}

@end
